/*
 * Copyright 1999-2018 Alibaba Group Holding Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.timanetworks.comu.utils;


import com.timanetworks.comu.consts.ResultConstant;

import java.io.Serializable;

/**
 * @author leyou
 * @author Eric Zhao
 */
public class Result<R> implements Serializable {
	/**
	 * 响应状态
	 */
	private boolean success;
	/**
	 * 响应编码
	 */
	private String code;
	/**
	 * 消息体
	 */
	private String message;
	/**
	 * data数据
	 */
	private R data;

	public static <R> Result<R> ofSuccess() {
		return new Result<R>().setSuccess(true).setCode(ResultConstant.success_code).setMsg(ResultConstant.success_msg);
	}
	public static <R> Result<R> ofSuccess(String msg) {
		return new Result<R>().setSuccess(true).setCode(ResultConstant.success_code).setMsg(msg);
	}
	public static <R> Result<R> ofSuccess(R data) {
		return new Result<R>().setSuccess(true).setCode(ResultConstant.success_code).setMsg(ResultConstant.success_msg).setData(data);
	}
	public static <R> Result<R> ofFail() {
		return new Result<R>().setSuccess(false).setCode(ResultConstant.fail_code).setMsg(ResultConstant.fail_msg);
	}
	public static <R> Result<R> ofFailMsg(String msg) {
		return new Result<R>().setSuccess(false).setCode(ResultConstant.fail_code).setMsg(msg);
	}
	public static <R> Result<R> ofFail(String code) {
		return new Result<R>().setSuccess(false).setCode(code).setMsg(ResultConstant.fail_msg);
	}
	public static <R> Result<R> ofFail(R data) {
		return new Result<R>().setSuccess(false).setCode(ResultConstant.fail_code).setMsg(ResultConstant.fail_msg).setData(data);
	}
	public static <R> Result<R> ofFail(String code, String msg,R data) {
		return new Result<R>().setSuccess(false).setCode(code).setMsg(msg).setData(data);
	}

	public static <R> Result<R> ofThrowable(String code, Throwable throwable) {
		Result<R> result = new Result<>();
		result.setSuccess(false);
		result.setCode(code);
		result.setMsg(throwable.getClass().getName() + ", " + throwable.getMessage());
		return result;
	}
	public static <R> Result<R> ofThrowable(Throwable throwable) {
		Result<R> result = new Result<>();
		result.setSuccess(false);
		result.setCode(ResultConstant.fail_code);
		result.setMsg(throwable.getClass().getName() + ", " + throwable.getMessage());
		return result;
	}

	public boolean isSuccess() {
		return success;
	}
	public boolean isFail() {
		return ! success;
	}

	public Result<R> setSuccess(boolean success) {
		this.success = success;
		return this;
	}

	public String getCode() {
		return code;
	}

	public Result<R> setCode(String code) {
		this.code = code;
		return this;
	}

	public String getMsg() {
		return message;
	}

	public Result<R> setMsg(String msg) {
		this.message = msg;
		return this;
	}

	public R getData() {
		return data;
	}

	public Result<R> setData(R data) {
		this.data = data;
		return this;
	}

	@Override
	public String toString() {
		return "Result{" + "success=" + success + ", code=" + code + ", msg='" + message + '\'' + ", data=" + data + '}';
	}

}
